package org.ljx.taobao.dao.impl;

import org.ljx.taobao.api.entity.Shopping;
import org.ljx.taobao.dao.IShoppingDao;
import org.ljx.taobao.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Ljx
 * @Date: 2021/12/31 11:23
 * @role: 购物车dao层实现
 */
public class ShoppingDaoImpl implements IShoppingDao {

    private final Connection conn = DruidUtil.getConn();

    /**
     * 购买商品
     *
     * @param shopping 订单实体
     * @return boolean
     */
    @Override
    public boolean commodity(Shopping shopping) {
        boolean z = false;
        try {
            String sql = "insert into shopping values(null,?,?,?,?,?)";
            String sql1 = "select * from shopping";
            String sql2 = "update shopping set num = ? where name = ?";
            PreparedStatement pst = conn.prepareStatement(sql);
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            while (rs.next()) {
                String name = rs.getString("name");
                int uid = rs.getInt("uid");
                int num = rs.getInt("num");
                if (name.equals(shopping.getName()) && uid == UserDaoImpl.uid) {
                    num++;
                    PreparedStatement ps = conn.prepareStatement(sql2);
                    ps.setInt(1, num);
                    ps.setString(2, shopping.getName());
                    ps.execute();
                    z = true;
                    return true;
                }
            }
            if (z != true) {
                pst.setString(1, shopping.getName());
                pst.setString(2, shopping.getInformation());
                pst.setInt(3, shopping.getPrice());
                pst.setInt(4, shopping.getNum());
                pst.setInt(5, UserDaoImpl.uid);
            }
            int len = pst.executeUpdate();
            if (len > 0) {
                return true;
            }
            pst.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static int x = 0;

    /**
     * 查询用户购买的商品
     *
     * @return List<Shopping>
     */
    @Override
    public List<Shopping> query() {
        List<Shopping> list = new ArrayList<>();
        try {
            String sql = "select * from shopping";
            java.sql.Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while (rs.next()) {
                int uid = rs.getInt("uid");
                if (uid == UserDaoImpl.uid) {
                    int id = rs.getInt("id");
                    String name = rs.getString("name");
                    String information = rs.getString("information");
                    int price = rs.getInt("price");
                    int num = rs.getInt("num");
                    Shopping shopping = new Shopping(id, name, information, price, num);
                    list.add(shopping);
                    x += price * num;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }

    /**
     * 删除订单
     *
     * @param shopping 订单实体
     * @return boolean
     */
    @Override
    public boolean delete(Shopping shopping) {
        try {

            String sql = "delete from shopping where id=?";
            String sql1 = "select * from shopping";
            java.sql.Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            PreparedStatement pr = conn.prepareStatement(sql);
            while (rs.next()) {
                int id = rs.getInt("id");
                int uid = rs.getInt("uid");
                if (shopping.getId() == id && uid == UserDaoImpl.uid) {
                    pr.setInt(1, id);
                    pr.execute();
                    return true;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 清除购物车
     *
     * @param shopping 订单实体
     * @return boolean
     */
    @Override
    public boolean clear(Shopping shopping) {
        try {


            String sql = "delete from shopping where uid =?";
            String sql1 = "select * from shopping";
            PreparedStatement pr = conn.prepareStatement(sql);
            java.sql.Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            while (rs.next()) {
                int uid = rs.getInt("uid");
                if (UserDaoImpl.uid == uid) {
                    pr.setInt(1, uid);
                    pr.execute();
                    return true;
                }
            }
            pr.execute();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 订单数量 -1
     *
     * @param shopping 订单实体
     * @return boolean
     */
    @Override
    public boolean decrease(Shopping shopping) {
        try {


            String sql1 = "select * from shopping";
            String sql2 = "update shopping set num = ? where id = ?";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            while (rs.next()) {
                int id = rs.getInt("id");
                int uid = rs.getInt("uid");
                int num = rs.getInt("num");
                if (id == shopping.getId() && uid == UserDaoImpl.uid) {
                    num--;
                    if (num == 0) {
                        return false;
                    } else {
                        PreparedStatement ps = conn.prepareStatement(sql2);
                        ps.setInt(1, num);
                        ps.setInt(2, id);
                        ps.execute();
                    }
                }
            }
            rs.close();
            st.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 订单数量 +1
     *
     * @param shopping 订单实体
     * @return boolean
     */
    @Override
    public boolean increase(Shopping shopping) {
        try {
            String sql1 = "select * from shopping";
            String sql2 = "update shopping set num = ? where id = ?";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql1);
            while (rs.next()) {
                int id = rs.getInt("id");
                int uid = rs.getInt("uid");
                int num = rs.getInt("num");
                if (id == shopping.getId() && uid == UserDaoImpl.uid) {
                    num++;
                    if (num == 0) {
                        return false;
                    } else {
                        PreparedStatement ps = conn.prepareStatement(sql2);
                        ps.setInt(1, num);
                        ps.setInt(2, id);
                        ps.execute();
                    }
                }
            }
            rs.close();
            st.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}
